package com.gonetbar.base.aop;

import java.util.Date;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;

import com.gonetbar.base.mail.SpeedAdminJavaMailSender;
import com.gonetbar.base.vo.SpeedAdminEmailMessage;

/**
 * 
 * @Description: 
 * 
 * @author weisd Email:xiyangdewuse@163.com
 * @date 2011-4-17 上午10:23:51
 * @version v1.0
 */
public class DaoTimeoutInterceptor implements MethodInterceptor {
	private static final Logger logger = Logger.getLogger("warnlogfile");
	private boolean listen = false;
	private int initWaringTime = 5000;
	private SpeedAdminJavaMailSender sqlWaringJavaMailSender;
	private SpeedAdminEmailMessage sem;



	public Object invoke(MethodInvocation arg0) throws Throwable {
		
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		System.out.println("-------------------测试 logger------------------");
		
		Object [] args = arg0.getArguments();
		Date before = null;
		if(listen == true){
			before = new Date();
		}
		Object obj = arg0.proceed();
		if(listen == true){
			Date after = new Date();
			int woTime = (int) (after.getTime()-before.getTime());
			if(woTime>initWaringTime){
				//this.writeLog((ParamObjVObj)args[0], woTime);
				logger.warn("-------------------测试 logger------------------");
			}
		}
		return obj;
	}
	
//	private void writeLog(ParamObjVObj paramObject,int howTime){
//		StringBuffer logInfo = new StringBuffer();
//		logInfo.append("DataSource:"); logInfo.append(paramObject.getDataSourceMapping()); logInfo.append(" Sqlid:"); logInfo.append(paramObject.getSqlId()); logInfo.append(" SQL:");
//		logInfo.append(paramObject.getSql());
//		logInfo.append(" Message: SQL executing wear-out time beyond > ");
//		logInfo.append(this.initWaringTime);
//		logInfo.append(" ms in program!!achieved wear-out time:");
//		logInfo.append(howTime);
//		waringLog1.warn(logInfo.toString());
//		this.sem.setText(logInfo.toString());
//		sqlWaringJavaMailSender.senderMail(this.sem);
//	}

	public int getInitWaringTime() {
		return initWaringTime;
	}

	public void setInitWaringTime(int initWaringTime) {
		this.initWaringTime = initWaringTime;
	}

	public boolean isListen() {
		return listen;
	}

	public void setListen(boolean listen) {
		this.listen = listen;
	}

	public SpeedAdminJavaMailSender getSqlWaringJavaMailSender() {
		return sqlWaringJavaMailSender;
	}

	public void setSqlWaringJavaMailSender(
			SpeedAdminJavaMailSender sqlWaringJavaMailSender) {
		this.sqlWaringJavaMailSender = sqlWaringJavaMailSender;
	}
}
